package com.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author：zhangzhanheng
 * @createTime: 2021/11/5  11:21 上午
 * @desc: 脱敏组件
 * @version: 1.0.0
 **/
public class HidenUtil {

    /** 半角空格 */
    private static final String BLANK_STRING         = " ";

    /** 字符串转换空格的pattern */
    private static final Pattern STRING_TO_BLANK_PATTERN = Pattern
            .compile("\\s|\t|\r|\n|·|\\.|\u2022|\u3000");

    /** 是否是合法邮箱 */
    private static final Pattern IS_VALID_EMAIL_PATTERN  = Pattern
            .compile("^([a-zA-Z0-9_\\.\\-\\+])+\\@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,20}$");

    /** 是否是合法手机号 */
    private static final Pattern IS_VALID_MOBILE_PATTERN = Pattern
            .compile("^[1](3|4|5|7|8)[0-9]{9}$");

    /** 英文字符和空格 */
    private static final String  englishWordsRegex       = "^[a-zA-Z\\s]*";

    /** 姓名掩码 */
    public static final String  MUSK_NAME            = "*";

    /** 账号掩码 */
    public static final String  MUSK_ACCOUNT         = "***";

    /**
     * 判断是否包含掩码
     * @param testString 测试字符串
     * @return 是否包含掩码
     */
    public static boolean containMusk(String testString) {
        return testString.contains(MUSK_NAME);
    }

    /**
     * 判断是否为邮箱
     * @param loginId
     * @return 是否邮箱
     */
    public static boolean isEmail(String loginId) {
        return isValidEmail(loginId);
    }

    /**
     * 判断是否手机号
     *
     * @param loginId 登录号
     * @return 是否手机号
     */
    public static boolean isMobileNo(String loginId) {
        return isValidMobile(loginId) || isValidForeignMobile(loginId);
    }

    /**
     * 获取脱敏的登录帐号
     * 邮箱帐号
     * 前面的字符只显示3位，不足3位，显示实际位数，后面加***
     *
     * 示例：
     * 如果账户为 tjyihui@126.com 则显示：tjy***@126.com
     * 如果账户为 mm@hotmail.com 则显示： mm***@hotmail.com
     * 如果账户为  iceziling@netvigator.com 则显示：ice***@netviga.com
     *
     * 手机账号
     * 大陆：显示前3位+***+后4位
     * 香港、澳门：显示前2位+***+后2位
     * 台湾：显示前2位+***+后3位
     * 示例：
     * 大陆： 186***1005
     * 香港、澳门： 90***85
     * 台湾：  90***856
     * @param loginId
     * @return
     */
    public static String getHidedLogonId(String loginId) {
        if (StringUtils.isBlank(loginId)) {
            return StringUtils.EMPTY;
        }

        // 如已脱敏，直接返回
        if (containMusk(loginId)) {
            return loginId;
        }

//        if (isEmail(loginId)) {
//            StringBuilder result = new StringBuilder();
//            //取@前的字符，标记长度
//            int indexa = StringUtil.indexOf(loginId, "@");
//            int left = indexa;
//            if (left > 3) {
//                result.append(StringUtil.left(loginId, 3)).append(MUSK_ACCOUNT).append("@");
//            } else {
//                result.append(StringUtil.left(loginId, left)).append(MUSK_ACCOUNT).append("@");
//            }
//
//            //取@和.间的字符，标记长度
//            String stringAftera = StringUtil.substringAfter(loginId, "@");
//            result.append(stringAftera);
//            return result.toString();
//        } else if (isMobileNo(loginId)) {
//            // 国内手机号
//            return StringUtil.overlay(loginId, MUSK_ACCOUNT, 3, 7);
//        } else {
//            return StringUtil.overlay(loginId, MUSK_ACCOUNT, 2, 6);
//        }
        return null;
    }

    public static boolean isValidEmail(String src) {
        if (StringUtils.isBlank(src)) {
            return false;
        } else {
            Matcher pm = IS_VALID_EMAIL_PATTERN.matcher(src);
            return pm.matches();
        }
    }

    public static boolean isValidMobile(String src) {
        if (StringUtils.isBlank(src)) {
            return false;
        } else {
            Matcher pm = IS_VALID_MOBILE_PATTERN.matcher(src);
            return pm.matches();
        }
    }

    public static boolean isValidForeignMobile(String src) {
        return StringUtils.isBlank(src) ? false
                : RegisterSupportMobileArea.isValidMobileNumber(src);
    }

    public static enum RegisterSupportMobileArea {
        CN(1, "中国大陆", Pattern.compile("^(86){0,1}\\-?1[3,4,5,7,8]\\d{9}$"), "86", "CN"),
        HK(2, "香港", Pattern.compile("^(852){1}\\-?0{0,1}[1,5,6,9](?:\\d{7}|\\d{8}|\\d{12})$"), "852", "HK"),
        MO(3, "澳门", Pattern.compile("^(853){1}\\-?6\\d{7}$"), "853", "MO"),
        TW(4, "台湾", Pattern.compile("^(886){1}\\-?0{0,1}[6,7,9](?:\\d{7}|\\d{8}|\\d{10})$"), "886", "TW"),
        KR(5, "韩国", Pattern.compile("^(82){1}\\-?0{0,1}[7,1](?:\\d{8}|\\d{9})$"), "82", "KR"),
        JP(6, "日本", Pattern.compile("^(81){1}\\-?0{0,1}[7,8,9](?:\\d{8}|\\d{9})$"), "81", "JP"),
        US(7, "美国", Pattern.compile("^(1){1}\\-?\\d{10,12}$"), "1", "US"),
        CA(8, "加拿大", Pattern.compile("^(1){1}\\-?\\d{10}$"), "1", "CA"),
        GB(9, "英国", Pattern.compile("^(44){1}\\-?[347-9](\\d{8,9}|\\d{11,12})$"), "44", "GB"),
        AU(10, "澳大利亚", Pattern.compile("^(61){1}\\-?4\\d{8,9}$"), "61", "AU"),
        SG(11, "新加坡", Pattern.compile("^(65){1}\\-?[13689]\\d{6,7}$"), "65", "SG"),
        MY(12, "马来西亚", Pattern.compile("^(60){1}\\-?1\\d{8}$"), "60", "MY"),
        TH(13, "泰国", Pattern.compile("^(66){1}\\-?[13456789]\\d{7,8}$"), "66", "TH"),
        VN(14, "越南", Pattern.compile("^(84){1}\\-?[1-9]\\d{6,9}$"), "84", "VN"),
        PH(15, "菲律宾", Pattern.compile("^(63){1}\\-?[24579](\\d{7,9}|\\d{12})$"), "63", "PH"),
        ID(16, "印度尼西亚", Pattern.compile("^(62){1}\\-?[2-9]\\d{7,11}$"), "62", "ID"),
        DE(17, "德国", Pattern.compile("^(49){1}\\-?1(\\d{5,6}|\\d{9,12})$"), "49", "DE"),
        IT(18, "意大利", Pattern.compile("^(39){1}\\-?[37]\\d{8,11}$"), "39", "IT"),
        FR(19, "法国", Pattern.compile("^(33){1}\\-?[168](\\d{5}|\\d{7,8})$"), "33", "FR"),
        RU(20, "俄罗斯", Pattern.compile("^(7){1}\\-?[13489]\\d{9,11}$"), "7", "RU"),
        NZ(21, "新西兰", Pattern.compile("^(64){1}\\-?[278]\\d{7,9}$"), "64", "NZ"),
        NL(22, "荷兰", Pattern.compile("^(31){1}\\-?6\\d{8}$"), "31", "NL"),
        SE(23, "瑞典", Pattern.compile("^(46){1}\\-?[124-7](\\d{8}|\\d{10}|\\d{12})$"), "46", "SE"),
        UA(24, "乌克兰", Pattern.compile("^(380){1}\\-?[3-79]\\d{8,9}$"), "380", "UA");

        int areaId;
        String areaName;
        Pattern checkKey;
        String code;
        String domainAbbreviation;

        private RegisterSupportMobileArea(int selIndex, String name, Pattern checkPattern, String areaCode, String abbreviation) {
            this.areaId = selIndex;
            this.areaName = name;
            this.checkKey = checkPattern;
            this.code = areaCode;
            this.domainAbbreviation = abbreviation;
        }

        public int getAreaId() {
            return this.areaId;
        }

        public String getAreaName() {
            return this.areaName;
        }

        public Pattern getCheckKey() {
            return this.checkKey;
        }

        public String getCode() {
            return this.code;
        }

        public String getDomainAbbreviation() {
            return this.domainAbbreviation;
        }

        public static RegisterSupportMobileArea domainAbbreviationOf(String abbreviation) {
            RegisterSupportMobileArea[] arr$ = values();
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                RegisterSupportMobileArea a = arr$[i$];
                if(a.domainAbbreviation.equalsIgnoreCase(abbreviation)) {
                    return a;
                }
            }

            return null;
        }

        public static boolean isValidMobileNumber(String src, String area) {
            return domainAbbreviationOf(area).getCheckKey().matcher(src).matches();
        }

        public static boolean isValidMobileNumber(String src) {
            RegisterSupportMobileArea[] arr$ = values();
            int len$ = arr$.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                RegisterSupportMobileArea a = arr$[i$];
                if(a.checkKey.matcher(src).matches()) {
                    return true;
                }
            }

            return false;
        }
    }

}